<!-- 贸易方式 -->
<template>
  <div class="page-container">
    <div class="in-container">
      <AppSearchForm @submit="getList" @reset="reset">
        <el-form-item>
          <el-input
            v-model="query.Label"
            clearable
            placeholder="请输入标签名称"
            style="width: 300px"
          />
        </el-form-item>
      </AppSearchForm>

      <el-button @click="openDialog" type="primary">新增</el-button>
      <el-button @click="deleteSelectData" type="danger"> 删除 </el-button>

      <AppPageTable
        stripe
        :data="dicData.data"
        @change="getList()"
        :autostyle="'height: calc(100% - 80px);'"
        :option="dicData.option"
      >
        <el-table-column
          show-overflow-tooltip
          type="selection"
          width="55"
          fixed="left"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="label"
          width="100"
          label="字典标签"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="value"
          width="150"
          label="字典值"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="dictSort"
          width="120"
          label="排序"
        />

        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button @click.stop="update(scope.row)" type="primary"
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除本条数据吗？"
              @confirm="deleteData(scope.row)"
            >
              <template #reference>
                <el-button type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </AppPageTable>
      <el-dialog
        :title="dialogData.title"
        v-model="dialogData.show"
        width="500px"
      >
        <el-form
          :model="formData"
          :rules="formRules"
          label-width="80px"
          ref="form"
        >
          <el-form-item prop="label" label="字典标签">
            <el-input v-model="formData.label"></el-input>
          </el-form-item>
          <el-form-item prop="value" label="字典值">
            <el-input v-model="formData.value"></el-input>
          </el-form-item>
          <el-form-item prop="dicSort" label="排序">
            <el-input-number
              v-model="formData.dictSort"
              :min="1"
              :max="999"
              controls-position="right"
            ></el-input-number>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="cancel" link>取消</el-button>
            <el-button type="primary" @click="submit"> 确认 </el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { defineComponent } from "vue";
import AppSearchForm from "@/components/AppSearchForm.vue";
import AppPageTable, { useTableOption } from "@/components/AppPageTable2";
import AppTitleBox from "@/components/AppTitleBox.vue";
import { ElMessage, ElMessageBox } from "element-plus";

export default defineComponent({
  components: {
    AppSearchForm,
    AppPageTable,
    AppTitleBox,
  },
  props: {
    name: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      title: "",
      query: {
        KeyWords: "",
        Label: "",
        DictName: "",
      },
      loading: false,
      dicData: {
        data: [],
        option: useTableOption(),
      },
      dialogData: {
        show: false,
        title: "",
        btnLoading: false,
      },
      formData: {
        id: "",
        name: this.name,
        label: "", // 字典标签
        value: "", // 字典值
        dictSort: "", // 排序
      },
      editData: {
        //编辑数据
        belong: "",
        createBy: "",
        createTime: "",
        dict: { id: "" },
        dictSort: "",
        id: "",
        index: 0,
        label: "",
        updateBy: "",
        updateTime: "",
        value: "",
      },
      formRules: {
        label: [{ required: true, message: "请输入字典标签", trigger: "blur" }],
        value: [{ required: true, message: "请输入字典值", trigger: "blur" }],
        dictSort: [
          { required: true, message: "请输入字典排序", trigger: "blur" },
        ],
      },
      options: [],
    };
  },
  computed: {
    data: {
      get() {
        return this.props.name;
      },
      set(val) {
        return val;
      },
    },
  },
  methods: {
    async getList() {
      this.dicData.option.loading = true;

      let res = await this.$http.get("/api/dictDetail/query", {
        PageIndex: this.dicData.option.page,
        PageSize: this.dicData.option.pageSize,
        DictName: this.name,
        Label: this.query.Label,
      });

      console.log(res);

      if (res.totalElements) {
        if (res.content[0].dict.id) {
          this.editData.dict.id = res.content[0].dict.id;
        }

        this.dicData.data = res.content;
        this.dicData.data.forEach((e) => {
          e["name"] = this.name;
        });
        this.dicData.option.total = res.totalElements;
        this.dicData.option.loading = false;
      } else {
        this.dicData.data = res.content;
        this.dicData.option.total = res.totalElements;
        this.dicData.option.loading = false;
      }
    },
    update(data) {
      this.formData.label = data.label;
      this.formData.value = data.value;
      this.formData.dictSort = data.dictSort;
      this.editData.belong = data.name;
      this.editData.createBy = data.createBy;
      this.editData.createTime = data.createTime;
      this.editData.dict.id = data.dict.id;
      this.editData.id = data.id;
      this.editData.index = data.index;
      this.editData.updateBy = data.updateBy;
      this.editData.updateTime = data.updateTime;
      this.editData.dict.id = data.dict.id;

      this.dialogData.title = "编辑字典";
      this.dialogData.show = true;
    },
    deleteData(data) {
      this.$http
        .post("/api/dictDetail/delete", {
          id: data.id,
        })
        .then((res) => {
          if (res.status) {
            this.$message.success(res.message);
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        });
    },
    async submit() {
      this.dialogData.btnLoading = true;
      await this.$refs.form.validate();
      if (this.editData.id) {
        this.$http
          .post("/api/dictDetail/edit", {
            belong: this.editData.belong,
            createBy: this.editData.createBy,
            createTime: this.editData.createTime,
            dict: this.editData.dict,
            dictSort: this.formData.dictSort,
            id: this.editData.id,
            index: this.editData.index,
            label: this.formData.label,
            updateBy: this.editData.updateBy,
            updateTime: this.editData.updateTime,
            value: this.formData.value,
          })
          .then((res) => {
            if (res.status) {
              this.$message.success(res.message);
              this.dialogData.show = false;
            } else {
              this.$message.error(res.message);
            }
            this.dialogData.btnLoading = false;
            this.getList();
          });
      } else {
        this.$http
          .post("/api/dictDetail/create", {
            dict: this.editData.dict,
            dictSort: this.formData.dictSort,
            label: this.formData.label,
            value: this.formData.value,
          })
          .then((res) => {
            if (res.status) {
              this.$message.success(res.message);
              this.getList();
              this.dialogData.show = false;
            } else {
              this.$message.error(res.message);
            }
            this.dialogData.btnLoading = false;
            this.getList();
          });
      }
    },
    /**
     * 多项删除业务
     */
    deleteSelectData() {
      let deleteSelectArray = [];
      this.dicData.option.selected.forEach((t) => deleteSelectArray.push(t.id));
      ElMessageBox.confirm("确定删除所选服务项目", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.$http
            .post("/api/dictDetail/deletes", {
              idArray: deleteSelectArray,
            })
            .then((res) => {
              if (res.status) {
                this.$message.success(res.message);
                this.getList();
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message.info("取消删除");
        });
    },
    reset() {
      //重置按钮点击事件
      this.query.KeyWords = "";
      this.query.Label = "";
      this.getList();
    },
    cancel() {
      this.dialogData.show = false;
    },
    openDialog() {
      this.editData.id = "";
      this.dialogData.title = "新增字典";
      this.dialogData.show = true;
    },
  },
  watch: {
    "dialogData.show"(newVal) {
      if (!newVal) {
        this.formData = {
          nationCode: "",
          nationName: "",
          nationEnName: "",
          customCode: "",
        };
        this.$nextTick(() => {
          this.$refs.form.clearValidate();
        });
      }
    },
  },
  mounted() {
    this.getList();
  },
});
</script>

<style lang="scss" scoped>
.form .el-select--small {
  width: 100px !important;
}
::v-deep .page_component .el-select--small {
  width: 100px !important;
}
</style>
